package com.power.sifa.std.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.std.model.Std_comword;
import com.power.sifa.std.model.pageInfo.Std_comwordPageInfo;
import com.power.sifa.std.api.IStd_comwordFeign;
import com.power.sifa.std.service.Std_comwordService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   常用语控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Std_comwordController",tags={"常用语"})
public class Std_comwordController implements IStd_comwordFeign{

    @Resource
    private Std_comwordService std_comwordService;


    @Override
    @ApiOperation("条件查询常用语")
    @SaCheckPermission("std_comword_queryBy")
    public ResponseDTO queryList(String content, String createuserid) {
        List<Std_comword> result = this.std_comwordService.queryList(content, createuserid);
        return ResponseDTO.succData(result);
    }

    @Override
    @ApiOperation("查询常用语分页列表")
    @SaCheckPermission("std_comword_list")
    public ResponseDTO list(Std_comwordPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Std_comword> page = this.std_comwordService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Std_comword std_comword : page) {
                std_comword.geneText();
                list.add(std_comword);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看常用语")
    @SaCheckPermission("std_comword_view")
    public ResponseDTO view(Integer id) {
        Assert.notNull(id, "id不能为空");
        Std_comword std_comword  = std_comwordService.getById(id);
        if (std_comword != null) {
            std_comword.geneText();
        }
        return ResponseDTO.succData(std_comword);
    }

    @Override
    @ApiOperation("删除常用语")
    @SaCheckPermission("std_comword_del")
    public ResponseDTO delete(Integer id) {
        Assert.notNull(id, "id不能为空");
        std_comwordService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改常用语")
    @SaCheckPermission("std_comword_save")
    public ResponseDTO save(Std_comword std_comword) {
        Assert.notNull(std_comword, "std_comword不能为空");
        std_comwordService.saveOrUpdate(std_comword);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除常用语")
    @SaCheckPermission("std_comword_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        std_comwordService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("std_comword_list")
    public void output(HttpServletResponse response, Std_comwordPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "常用语";
        String[] heads = {"序号","类别","内容","创建人","顺序号","状态"};
        String[] fields = {"id","category","content","createuserid","sortnum","status"};
        Page<Std_comword> page = this.std_comwordService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "常用语";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","类别","内容","创建人","顺序号","状态"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","category","content","createuserid","sortnum","status"};
        List<Std_comword> list = (List<Std_comword>)ExcelUtil.Import(fields, file, Std_comword.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.std_comwordService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

}
